import json
from Web.WebClassCongregation import NistData
from ClassCongregation import ErrorLog
from Web.WebClassCongregation import UserInfo
from django.http import JsonResponse
from Web.Workbench.LogRelated import UserOperationLogRecord,RequestLogRecord


"""nist_data_bulk_query
{
	"token": "xxx",
	"number_of_pages":"20"
}
"""
def NistDataBulkQuery(request):#查询Nist的基础数据监控数据
    RequestLogRecord(request, request_api="nist_data_bulk_query")
    if request.method == "POST":
        try:
            Token=json.loads(request.body)["token"]
            NumberOfPages = json.loads(request.body)["number_of_pages"]#页数
            Uid = UserInfo().QueryUidWithToken(Token)  # 如果登录成功后就来查询UID
            if Uid != None:  # 查到了UID
                UserOperationLogRecord(request, request_api="nist_data_bulk_query", uid=Uid)  # 查询到了在计入
                if int(NumberOfPages)<0:
                    return JsonResponse({'message': "你家有小于0的页码？", 'code': 503, })
                else:
                    SearchResult=NistData().BulkQuery(number_of_pages=int(NumberOfPages))#获取数据
                    return JsonResponse({'message': SearchResult, 'code': 200, })
            else:
                return JsonResponse({'message': "小宝贝这是非法查询哦(๑•̀ㅂ•́)و✧", 'code': 403, })
        except Exception as e:
            ErrorLog().Write("Web_CommonVulnerabilitiesAndExposuresMonitor_VulnerabilityNumberMonitoring_Nist_NistDataBulkQuery(def)", e)
    else:
        return JsonResponse({'message': '请使用Post请求', 'code': 500, })

"""nist_data_detailed_query
{
	"token": "xxx",
	"common_vulnerabilities_and_exposures":"CVE-2021-3177"
}
"""
def NistDataDetailedQuery(request):#查询单个CVE细节数据
    RequestLogRecord(request, request_api="nist_data_detailed_query")
    if request.method == "POST":
        try:
            Token=json.loads(request.body)["token"]
            CommonVulnerabilitiesAndExposures = json.loads(request.body)["common_vulnerabilities_and_exposures"]#CVE编号
            Uid = UserInfo().QueryUidWithToken(Token)  # 如果登录成功后就来查询UID
            if Uid != None:  # 查到了UID
                UserOperationLogRecord(request, request_api="nist_data_detailed_query", uid=Uid)  # 查询到了在计入
                SearchResult=NistData().DetailedQuery(common_vulnerabilities_and_exposures=CommonVulnerabilitiesAndExposures)#获取数据
                return JsonResponse({'message': SearchResult, 'code': 200, })
            else:
                return JsonResponse({'message': "小宝贝这是非法查询哦(๑•̀ㅂ•́)و✧", 'code': 403, })
        except Exception as e:
            ErrorLog().Write("Web_CommonVulnerabilitiesAndExposuresMonitor_VulnerabilityNumberMonitoring_Nist_NistDataDetailedQuery(def)", e)
    else:
        return JsonResponse({'message': '请使用Post请求', 'code': 500, })

"""nist_statistics
{
	"token": "xxx"
}
"""
def NistStatistics(request):#对当前的CVE个数进行统计
    RequestLogRecord(request, request_api="nist_statistics")
    if request.method == "POST":
        try:
            Token=json.loads(request.body)["token"]
            Uid = UserInfo().QueryUidWithToken(Token)  # 如果登录成功后就来查询UID
            if Uid != None:  # 查到了UID
                UserOperationLogRecord(request, request_api="nist_statistics", uid=Uid)  # 查询到了在计入
                SearchResult=NistData().StatisticalData()#统计的个数
                return JsonResponse({'message': SearchResult, 'code': 200, })
            else:
                return JsonResponse({'message': "小宝贝这是非法查询哦(๑•̀ㅂ•́)و✧", 'code': 403, })
        except Exception as e:
            ErrorLog().Write("Web_CommonVulnerabilitiesAndExposuresMonitor_VulnerabilityNumberMonitoring_Nist_NistStatistics(def)", e)
    else:
        return JsonResponse({'message': '请使用Post请求', 'code': 500, })


"""nist_severity_filter
{
	"token": "xxx",
	"number_of_pages":"0",
	"severity":"xxxx"
}
"""
def NistSeverityFilter(request):#严重程度筛选查询
    RequestLogRecord(request, request_api="nist_severity_filter")
    if request.method == "POST":
        try:
            Token=json.loads(request.body)["token"]
            NumberOfPages = json.loads(request.body)["number_of_pages"]  # 页数
            Severity = json.loads(request.body)["severity"]  # 严重性等级
            Uid = UserInfo().QueryUidWithToken(Token)  # 如果登录成功后就来查询UID
            if Uid != None:  # 查到了UID
                UserOperationLogRecord(request, request_api="nist_severity_filter", uid=Uid)  # 查询到了在计入
                if int(NumberOfPages)<0:
                    return JsonResponse({'message': "你家有小于0的页码？", 'code': 503, })
                else:
                    FinalData = {}  # 最终包含漏洞个数和分页的数据
                    FinalData["total"] = NistData().ModuleDataStatistics(module_name="v3_base_severity",
                                                                         module_content=Severity)  # 查询漏洞总数
                    FinalData["data"] = NistData().SeverityQuery(number_of_pages=int(NumberOfPages),severity=Severity)  # 对查询分页数据

                    return JsonResponse({'message': FinalData, 'code': 200, })
            else:
                return JsonResponse({'message': "小宝贝这是非法查询哦(๑•̀ㅂ•́)و✧", 'code': 403, })
        except Exception as e:
            ErrorLog().Write("Web_CommonVulnerabilitiesAndExposuresMonitor_VulnerabilityNumberMonitoring_Nist_NistSeverityFilter(def)", e)
    else:
        return JsonResponse({'message': '请使用Post请求', 'code': 500, })


"""nist_vendors_filter
{
	"token": "xxx",
	"number_of_pages":"0",
	"vendors":"xxxx"
}
"""
def NistVendorsFilter(request):#厂商名称筛选器
    RequestLogRecord(request, request_api="nist_vendors_filter")
    if request.method == "POST":
        try:
            Token=json.loads(request.body)["token"]
            NumberOfPages = json.loads(request.body)["number_of_pages"]  # 页数
            Vendors = json.loads(request.body)["vendors"]  #厂商名称
            Uid = UserInfo().QueryUidWithToken(Token)  # 如果登录成功后就来查询UID
            if Uid != None:  # 查到了UID
                UserOperationLogRecord(request, request_api="nist_vendors_filter", uid=Uid)  # 查询到了在计入
                if int(NumberOfPages)<0:
                    return JsonResponse({'message': "你家有小于0的页码？", 'code': 503, })
                else:
                    FinalData={}#最终包含漏洞个数和分页的数据
                    FinalData["total"]=NistData().ModuleDataStatistics(module_name="vendors",module_content="%"+Vendors+"%")#查询漏洞总数
                    FinalData["data"]=NistData().VendorsQuery(number_of_pages=int(NumberOfPages),vendors=Vendors)#对查询分页数据
                    return JsonResponse({'message': FinalData, 'code': 200, })
            else:
                return JsonResponse({'message': "小宝贝这是非法查询哦(๑•̀ㅂ•́)و✧", 'code': 403, })
        except Exception as e:
            ErrorLog().Write("Web_CommonVulnerabilitiesAndExposuresMonitor_VulnerabilityNumberMonitoring_Nist_NistVendorsFilter(def)", e)
    else:
        return JsonResponse({'message': '请使用Post请求', 'code': 500, })

"""nist_products_filter
{
	"token": "xxx",
	"number_of_pages":"0",
	"products":"xxxx"
}
"""
def NistProductsFilter(request):#产品名称筛选器
    RequestLogRecord(request, request_api="nist_products_filter")
    if request.method == "POST":
        try:
            Token=json.loads(request.body)["token"]
            NumberOfPages = json.loads(request.body)["number_of_pages"]  # 页数
            Products = json.loads(request.body)["products"]  #厂商名称
            Uid = UserInfo().QueryUidWithToken(Token)  # 如果登录成功后就来查询UID
            if Uid != None:  # 查到了UID
                UserOperationLogRecord(request, request_api="nist_products_filter", uid=Uid)  # 查询到了在计入
                if int(NumberOfPages)<0:
                    return JsonResponse({'message': "你家有小于0的页码？", 'code': 503, })
                else:
                    FinalData = {}  # 最终包含漏洞个数和分页的数据
                    FinalData["total"] = NistData().ModuleDataStatistics(module_name="products",
                                                                         module_content="%" + Products + "%")  # 查询漏洞总数
                    FinalData["data"] = NistData().ProductsQuery(number_of_pages=int(NumberOfPages),products=Products)  # 对查询分页数据
                    return JsonResponse({'message': FinalData, 'code': 200, })
            else:
                return JsonResponse({'message': "小宝贝这是非法查询哦(๑•̀ㅂ•́)و✧", 'code': 403, })
        except Exception as e:
            ErrorLog().Write("Web_CommonVulnerabilitiesAndExposuresMonitor_VulnerabilityNumberMonitoring_Nist_NistProductsFilter(def)", e)
    else:
        return JsonResponse({'message': '请使用Post请求', 'code': 500, })